﻿//You can change this to feet your needs, just please keep giving credits to the author
//Thanks
//	DarkAngel  ( DarkAngel@IdleZone.Org )

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DarkRuler
{
	public partial class MyPictureBox : PictureBox, System.ComponentModel.ISupportInitialize
	{
		//Allow to know when picture is ok
		public bool IsPictureOk = false;
		//Allow to know if a picture was taken ^^
		public bool IsPictureTaken = false;

		private Color CBColor = Color.Red;
		private Color LineColor = Color.FromArgb(0, 255, 255);

		/// <summary>
		/// Red Box
		/// </summary>
		private RectangleF BoxDrawing = Rectangle.Empty;
		private float BoxRatio = 1;
		internal float GetBoxXSize()
		{
			return BoxDrawing.Width;
		}
		public void SetBoxSize(float X, float Y)
		{
			BoxDrawing.X = 10;
			BoxDrawing.Y = 10;
			BoxDrawing.Width = X;
			BoxDrawing.Height = Y;
			BoxRatio = ((float)BoxDrawing.Height) / ((float)BoxDrawing.Width);
		}
		public void ChangeBoxSize(double deltaValue)
		{
			if (!BoxDrawing.IsEmpty)
			{
				BoxDrawing.Width = BoxDrawing.Width * (1 + (float)deltaValue);
				BoxDrawing.Height = (int)(BoxRatio * BoxDrawing.Width);
			}
			this.Invalidate();
		}
		public void MoveBox(int X, int Y)
		{
			if (!BoxDrawing.IsEmpty)
			{
				BoxDrawing.X = X;
				BoxDrawing.Y = Y;
			}
			this.Invalidate();
		}
		/// <summary>
		/// Line start/stop
		/// </summary>
		public Point StartPoint = Point.Empty;
		public Point EndPoint = Point.Empty;

		private RectangleF PictureDrawing = Rectangle.Empty;
		private float ImageRatio = 1;
		public void ChangePictureSize(double deltaValue)
		{
			if (!PictureDrawing.IsEmpty)
			{
				PictureDrawing.Width = PictureDrawing.Width * (1 + (-1 * (float)deltaValue));
				PictureDrawing.Height = (int)(ImageRatio * PictureDrawing.Width);
			}
			this.Invalidate();
		}
		public void MovePicture(int X, int Y)
		{
			if (!PictureDrawing.IsEmpty)
			{
				PictureDrawing.X = X;
				PictureDrawing.Y = Y;
			}
			this.Invalidate();
		}

		private Bitmap _LastFile;
		public Bitmap LastFile
		{
			get { return _LastFile; }
			set
			{
				_LastFile = value;
				ImageRatio = ((float)_LastFile.Height) / ((float)_LastFile.Width);
				PictureDrawing.X = 0;
				PictureDrawing.Y = 0;
				PictureDrawing.Width = _LastFile.Width;
				PictureDrawing.Height = _LastFile.Height;
				Image = _LastFile;
				PictureDrawing.Width = ClientRectangle.Width;
				PictureDrawing.Height = (int)(ImageRatio * PictureDrawing.Width);
				Invalidate();
			}
		}

		public MyPictureBox()
		{
			LastFile = new Bitmap(468, 428, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			Bitmap buffer = new Bitmap(this.Width, this.Height);
			Graphics graphics = Graphics.FromImage(buffer);
			Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
			PaintEventArgs arg = new PaintEventArgs(graphics, rect);

			//paint in the buffer
			PaintEverythingInTheBuffer(arg);

			//paint on the screen
			e.Graphics.DrawImage(buffer, 0, 0);

			//clean
			graphics.Dispose();
			buffer.Dispose();
		}

		private void PaintEverythingInTheBuffer(PaintEventArgs arg)
		{
			arg.Graphics.Clear(Color.Transparent);
			Rectangle dstRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
			dstRect.Width = (int)PictureDrawing.Width;
			dstRect.Height = (int)(ImageRatio * dstRect.Width);
			dstRect.X -= (int)PictureDrawing.X;
			dstRect.Y -= (int)PictureDrawing.Y;
			Rectangle srcRect = new Rectangle(0, 0, Image.Width, Image.Height);
			arg.Graphics.DrawImage(LastFile, dstRect, srcRect, GraphicsUnit.Pixel);

			if (IsPictureOk)
			{
				arg.Graphics.DrawRectangle(new Pen(CBColor, 3), (int)BoxDrawing.X, (int)BoxDrawing.Y, (int)BoxDrawing.Width, (int)BoxDrawing.Height);
				Pen LinePen = new Pen(LineColor, 3);
				arg.Graphics.DrawEllipse(LinePen, StartPoint.X - 5, StartPoint.Y - 5, 10, 10);
				arg.Graphics.DrawLine(LinePen, StartPoint.X, StartPoint.Y, EndPoint.X, EndPoint.Y);
				arg.Graphics.DrawEllipse(LinePen, EndPoint.X - 5, EndPoint.Y - 5, 10, 10);
			}
		}
		protected override void OnPaintBackground(PaintEventArgs e)
		{
			//base.OnPaintBackground(e);
		}

		#region ISupportInitialize Members

		public void BeginInit()
		{
			//
		}

		public void EndInit()
		{
			//
		}

		#endregion

	}
}
